package com.jt.tenant.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.LinearLayout;

import com.jt.tenant.R;
import com.jt.tenant.app.JTApplication;

/**
 * Created by yzf on 2016/2/14.
 */
public class EquipInfoActivity extends JTActivity implements View.OnClickListener {

    private TextView title_name1,commit;

    private TextView tv_equip_connect_state;

    private TextView tv_equip_number, tv_equip_remaining_time;

    private LinearLayout all_equip_tv, lin_equip_wifi_state;

    public static RefreshDataListener refreshDataListener;

    private ImageView iv_equip_connect_state, iv_equip_wifi_state;

    private String state;

    private TextView tv_unbundling;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_equip_info);

        init_view();

        refreshDataListener = new RefreshDataListener() {
            @Override
            public void refreshData() {

                refresh_info();
//                finish();

            }
        };

        if (JTApplication.jtPreferences.readHaveObdid()) {

            state = "1";

        } else {

            state = "0";

        }
    }

    private void init_view() {

        (title_name1 = (TextView) findViewById(R.id.tv_title_name)).setText("设备管理");

        (commit = (TextView)findViewById(R.id.tv_equip_bind) ).setOnClickListener(this);

        tv_equip_connect_state = (TextView) findViewById(R.id.tv_equip_connect_state);

        tv_equip_number = (TextView) findViewById(R.id.tv_equip_number);

        tv_equip_remaining_time = (TextView) findViewById(R.id.tv_equip_remaining_time);

        all_equip_tv = (LinearLayout) findViewById(R.id.all_equip_tv);

        iv_equip_connect_state = (ImageView) findViewById(R.id.iv_equip_connect_state);

        iv_equip_wifi_state = (ImageView) findViewById(R.id.iv_equip_wifi_state);

        lin_equip_wifi_state = (LinearLayout)findViewById(R.id.lin_equip_wifi_state);

        tv_unbundling = (TextView)findViewById(R.id.tv_unbundling);

        tv_unbundling.setVisibility(View.GONE);

        refresh_info();

    }

    private void refresh_info() {

        if (!JTApplication.jtPreferences.readOdbID().equals("")) {

//            commit.setVisibility(View.GONE);

            commit.setText("解除绑定");

            tv_unbundling.setVisibility(View.VISIBLE);

            tv_equip_connect_state.setText("设备已连接");

            iv_equip_connect_state.setImageResource(R.mipmap.equip_on);

            lin_equip_wifi_state.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    startActivity(new Intent(EquipInfoActivity.this,WifiControlActivity.class));

                }
            });

            if (JTApplication.jtPreferences.readWIFI().equals("1")) {

                iv_equip_wifi_state.setImageResource(R.mipmap.equip_wifi_on);


            } else {

                iv_equip_wifi_state.setImageResource(R.mipmap.equip_wifi_off);

            }

            all_equip_tv.setVisibility(View.VISIBLE);

            if (JTApplication.jtPreferences.readOdbID().equals("null") || JTApplication.jtPreferences.readOdbID() == null || JTApplication.jtPreferences.readOdbID().equals("NULL")) {

                tv_equip_number.setText("");

            } else {

                tv_equip_number.setText(JTApplication.jtPreferences.readOdbID());

            }

        } else {

            commit.setText("绑定设备");

            tv_equip_connect_state.setText("设备未连接");

            iv_equip_connect_state.setImageResource(R.mipmap.equip_off);

            all_equip_tv.setVisibility(View.INVISIBLE);
        }
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()){

            case R.id.tv_equip_bind:

                startActivity(new Intent(this, ScannerActivity.class));

                break;


        }


    }

    public abstract class RefreshDataListener {

        public abstract void refreshData();

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == 0x11) {

            String tag = data.getStringExtra("tag");

        }
    }

}
